﻿using System;
using System.Collections.Generic;
using System.Data;

namespace ApplicationTemplate
{
    class CreateDacChangeAPI
    {
        public static String CreateChangeModel(String modelClassName, DataSet ds)
        {

            string fields = "";
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                String name = row["name"].ToString();
                String user_type_id = row["user_type_id"].ToString();

                fields += String.Format(@"            if(row.Table.Columns.Contains(""{0}""))", name);
                fields += "\r\n            {\r\n";

                //添加user_type_id=="58" 数据类型为smalldatetime型的判断
                if (user_type_id == "61" || user_type_id == "40"||user_type_id=="58")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowDateTime(""{0}"", row);", name);
                }
                //Add by wangsw 20121018 beign
                else if (user_type_id == "52")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowInt16(""{0}"", row);", name);
                }
                else if (user_type_id == "56")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowInt(""{0}"", row);", name);
                }
                else if (user_type_id == "127")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowLong(""{0}"", row);", name);
                }
                //Add by wangsw 20121018 end

                //Add by changqiguo 20121029
                else if (user_type_id == "106")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowDecimal(""{0}"", row);", name);
                }
                else if (user_type_id == "104")
                {
                    fields += String.Format(@"                 model.{0} = SicsUtility.GetDataRowBit(""{0}"", row);", name);
                }
                else
                {
                    fields += String.Format(@"                 model.{0} = row[""{0}""].ToString();", name);
                }
                fields += "\r\n            }\r\n";
            }

            return String.Format(
@"public static {0} ChangeModel(DataRow row)
        {{
            {0} model = new {0}();
{1}            return model;
        }}",
            modelClassName,
            fields
            );
        }

        public static String CreateChangeDs(String modelClassName)
        {

            return String.Format(
@"public static List<{0}> ChangeDataSet(DataSet ds)
        {{
            List<{0}> list = new List<{0}>();

            if (ds == null || ds.Tables.Count == 0)
            {{
                return list;
            }}

            foreach (DataRow row in ds.Tables[0].Rows)
            {{
                list.Add(ChangeModel(row));
            }}

            return list;
        }}",
            modelClassName
            );
        }
    }
}
